package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.BedMapper;
import com.zzyl.service.BedService;
import com.zzyl.vo.BedVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class BedServiceImpl implements BedService {

    @Autowired
    private BedMapper bedMapper;

    @Override
    public List<BedVo> getBedsByRoomId(Long roomId) {
        return bedMapper.getBedsByRoomId(roomId);
    }
    @Override
    public void createBed(BedDto dto) {
        //dto转entity
        Bed bed = BeanUtil.toBean(dto, Bed.class);
        bed.setBedStatus(0);
        bed.setCreateTime(LocalDateTime.now());
        bed.setUpdateTime(LocalDateTime.now());
        bed.setCreateBy(1L);
        bed.setUpdateBy(2L);
        //调用mapper层 保存床位
        try {
            bedMapper.insert(bed);
        } catch (Exception e) {
            throw new BaseException(BasicEnum.BED_INSERT_FAIL);
        }
    }

    @Override
    public BedVo readById(Long id) {
        //调用mapper查询床位信息
        Bed bed = bedMapper.selectById(id);
        //entity转vo
        BedVo bedVo = BeanUtil.toBean(bed, BedVo.class);
        return bedVo;
    }
    @Override
    public void updateBed(BedDto dto) {
        //dto转entity
        Bed bed = BeanUtil.toBean(dto, Bed.class);
        bed.setUpdateTime(LocalDateTime.now());
        bed.setUpdateBy(1L);
        //调用mapper修改
        bedMapper.update(bed);
    }

    @Override
    public void deleteById(Long id) {
        //校验床位是否存在  如果不存在则抛出异常
        Bed bed = bedMapper.selectById(id);
        if(bed==null){
            throw new BaseException(BasicEnum.BED_DELETE_FILE);
        }
        //调用mapper删除床位
       bedMapper.deleteById(id);
    }
}

